﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;


namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class BulidPaymentManage:BasePage
    {
        public BulidPaymentManage()
        {
            this.ModuleNo = Module.单据管理.ToString();
            this.PageOperateType = OperateType.结算单管理.ToString();
        }
        IBLL_Purchase ip = Factory.BLL_Purchase();
        int totalDrugrecord = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
               QueryData();
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            QueryData();
        }
        protected void QueryData()
        {
            HZ.DrugTrade.Model.Query.InvoicePay qryWhere = new HZ.DrugTrade.Model.Query.InvoicePay();
            qryWhere.InvoicePayNo = txtProjectNo.Text.Trim();
            qryWhere.InvoicePayName = txtDistriPlanName.Text.Trim();
            qryWhere.CompanyName_PS = txtCompanyName.Text.Trim();
            qryWhere.HospitalName = txtHospitalName.Text.Trim();
            switch (Convert.ToInt32(ddlPayStatus.SelectedValue.ToString().Trim()))
            {
                case -1:
                    break;
                case 0:
                    qryWhere.PayStatus = 0;
                    break;
                case 1:
                    qryWhere.PayStatus = 1;
                    break;
            }
            qryWhere.SortFields = "InvoicePayTime DESC";
            IList<Model.View.InvoicePay> lstInvoicePay = new List<Model.View.InvoicePay>();
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();
            lstInvoicePay = iPurchase.GetTotalInvoicePayByWhere(qryWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalDrugrecord);
            gvwProcurementProjectList.DataSource = lstInvoicePay;
            gvwProcurementProjectList.AutoGenerateColumns = false;
            gvwProcurementProjectList.DataBind();
            AspNetPager1.RecordCount = totalDrugrecord;

        }
        protected void gvwProcurementProjectList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblPayStatus = e.Row.FindControl("lblPayStatus") as Label;
                Label lblMod = e.Row.FindControl("lblMod") as Label;
                LinkButton lbnDel = e.Row.FindControl("lbtnDel") as LinkButton;
                if (lblPayStatus.Text == "0")
                {
                    lblPayStatus.Text = "未确认";
                }
                else if (lblPayStatus.Text == "1")
                {
                    lblPayStatus.Text = "已确认";
                }
            }
        }

        protected void gvwProcurementProjectList_SelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            QueryData();
        }

        protected void gvwProcurementProjectList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvwProcurementProjectList.EditIndex = e.NewEditIndex;
            QueryData();
        }

        protected void gvwProcurementProjectList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           if(Page.IsValid)
           {
            //TextBox txtInvoicePayNo = (TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtInvoicePayNo");
            TextBox txtgvInvoicePayName = (TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtgvInvoicePayName");
            TextBox txtInvoicePayTime = (TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtInvoicePayTime");
            //if (string.IsNullOrEmpty(txtInvoicePayNo.Text.Trim()))
            //{
            //    HZ.Web.WebUtil.Alert("结算单号不能为空！", this);
            //    ((TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtInvoicePayNo")).Focus();
            //    return;
            //}
            if(String.IsNullOrEmpty(txtgvInvoicePayName.Text.Trim()))
            {
                 HZ.Web.WebUtil.Alert("结算单名称不能为空！", this);
                ((TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtInvoicePayName")).Focus();
                return;
            }
           if(String.IsNullOrEmpty(txtInvoicePayTime.Text.Trim()))
           {
               HZ.Web.WebUtil.Alert("结算时间不能为空！", this);
               ((TextBox)gvwProcurementProjectList.Rows[e.RowIndex].FindControl("txtInvoicePayTime")).Focus();
               return;
           }
            InvoicePay invoicepay = new InvoicePay();
            invoicepay.InvoicePaySerialID = gvwProcurementProjectList.DataKeys[e.RowIndex].Values[0].ToString();
            //invoicepay.InvoicePayNo = txtInvoicePayNo.Text.Trim();
            invoicepay.InvoicePayName = txtgvInvoicePayName.Text.Trim();
            invoicepay.InvoicePayTime = Convert.ToDateTime(txtInvoicePayTime.Text.Trim());
            //User user = CurrentAccount.GetUser();
            invoicepay.Remarks = "Altered by " + CurrentAccount.GetUser().Org.OrgName;
            int nSuccessUpdate = ip.UpdateInvoicePay(invoicepay);
            if (nSuccessUpdate == 1)
            {
                HZ.Web.WebUtil.Alert("结算单已更新成功!", this);
                gvwProcurementProjectList.EditIndex = -1;
                QueryData();
            }
            else
            {
                HZ.Web.WebUtil.Alert("结算单更新失败!", this);
                return;
            }
           }
        }

        protected void gvwProcurementProjectList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwProcurementProjectList.EditIndex = -1;
            QueryData();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.InvoicePay qryWhere = new HZ.DrugTrade.Model.Query.InvoicePay();
            qryWhere.InvoicePayNo = txtProjectNo.Text.Trim();
            qryWhere.InvoicePayName = txtDistriPlanName.Text.Trim();
            qryWhere.CompanyName_PS = txtCompanyName.Text.Trim();
            qryWhere.HospitalName = txtHospitalName.Text.Trim();
            switch (Convert.ToInt32(ddlPayStatus.SelectedValue.ToString().Trim()))
            {
                case -1:
                    break;
                case 0:
                    qryWhere.PayStatus = 0;
                    break;
                case 1:
                    qryWhere.PayStatus = 1;
                    break;
            }
            qryWhere.SortFields = "InvoicePayTime DESC";
            IList<Model.View.InvoicePay> lstInvoicePay = new List<Model.View.InvoicePay>();
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();
            lstInvoicePay = iPurchase.GetTotalInvoicePayByWhere(qryWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalDrugrecord);
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.InvoicePay>(qryWhere, true);
            string sql = string.Format(@"SELECT DISTINCT  CompanyID_PS 配送企业编号
                                                ,CompanyName_PS 配送企业名称
                                                ,InvoicePayTime 结算时间
                                                ,PayAmount 应付金额
                                                ,ActualAmount 实付金额
                                                ,InvoicePayName 结算单名称
                                                ,HospitalName 医疗机构名称
                                                ,AreaID 地区编号
                                                ,HospitalID 医疗机构编号
                                                ,Remarks 备注 FROM View_InvoicePay  WHERE InvoicePaySerialID IS NOT NULL {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "结算信息.xls");
        }
    }
}
